package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Transactional
    @Override
    public void addSetMeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        //补充套餐数据
        BeanUtils.copyProperties(dto,setmeal);
        //新增套餐
        setMealMapper.addSetMeal(setmeal);
        //获取套餐菜品
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //为套餐菜品赋套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        //新增套餐菜品
        setMealDishMapper.addSetMeal(setmealDishes);
    }

    /**
     * 分页查询套餐
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pagesGetSet(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<SetmealVO> setMeal = setMealMapper.pagesGetSet(dto);
        return new PageResult(setMeal.getTotal(),setMeal.getResult());
    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSet(List<Long> ids) {
        //判断套餐是否可以被删除
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.getById(id);
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐
        setMealMapper.deleteSet(ids);
        //删除套餐菜品关联表
        setMealDishMapper.deleteSetDish(ids);
    }

    /**
     * 根据id查找套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //获取套餐
        Setmeal setmeal = setMealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //获取套餐菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param dto
     */
    @Transactional
    @Override
    public void modifySet(SetmealDTO dto) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setMealMapper.modifySet(setmeal);
        //删除套餐菜品
        List<Long> ids = new ArrayList<>();
        ids.add(setmeal.getId());
        setMealDishMapper.deleteSetDish(ids);
        //添加套餐菜品
        dto.getSetmealDishes().forEach(dish -> {
            dish.setSetmealId(setmeal.getId());
        });
        if(dto.getSetmealDishes()!=null && !dto.getSetmealDishes().isEmpty()){
            setMealDishMapper.addSetMeal(dto.getSetmealDishes());
        }
    }

    /**
     * 起售停售套餐
     *
     * @param status
     * @param id
     */
    @Override
    public void setMealStatus(Integer status, Long id) {
        List<SetmealDish> meals = setMealDishMapper.getBySetId(id);
        List<Long> dish = meals.stream().map(meal -> meal.getDishId()).collect(Collectors.toList());
        //查看是否有未售卖中的菜品
        for (Long dishId : dish) {
            Dish dish1 = dishMapper.getById(dishId);
            if(dish1.getStatus().equals(StatusConstant.DISABLE)){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        //补充数据，修改状态
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setMealMapper.modifySet(setmeal);
    }
}
